package sz.szsmk.citizencard.util.calender;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import androidx.core.content.ContextCompat;

import com.haibin.calendarview.Calendar;
import com.haibin.calendarview.MonthView;

import sz.szsmk.citizencard.R;

/**
 * 下标标记的日历控件
 * Created by huanghaibin on 2017/11/15.
 */

public class IndexMonthView extends MonthView {
    private Paint mSchemeBasicPaint = new Paint();
    private int mPadding;
    private int mH, mW;

    private Paint mPicPaint = new Paint();
    Bitmap bmpGou ;
    Bitmap bmpSign ;

    private Paint pointPaint = new Paint();

    private Paint serverTimePaint = new Paint();

    public IndexMonthView(Context context) {
        super(context);

        mSchemeBasicPaint.setAntiAlias(true);
        mSchemeBasicPaint.setStyle(Paint.Style.FILL);
        mSchemeBasicPaint.setTextAlign(Paint.Align.CENTER);
        mSchemeBasicPaint.setColor(0xff333333);
        mSchemeBasicPaint.setFakeBoldText(true);
        mPadding = dipToPx(getContext(), 4);
        mH = dipToPx(getContext(), 2);
        mW = dipToPx(getContext(), 8);

        bmpGou = BitmapFactory.decodeResource(getResources(), R.drawable.pic_gou);
        mPicPaint.setAntiAlias(true);

        bmpSign = BitmapFactory.decodeResource(getResources(), R.drawable.pic_sign);
        //设置日期下面积分 的笔
        pointPaint.setAntiAlias(true);
        pointPaint.setColor(ContextCompat.getColor(getContext(), R.color.color_f5a623));
        pointPaint.setTextAlign(Paint.Align.CENTER);
        pointPaint.setTextSize( dipToPx(getContext(), 12));

        serverTimePaint.setAntiAlias(true);
        serverTimePaint.setColor(ContextCompat.getColor(getContext(), R.color.color_FF0000));
        serverTimePaint.setTextAlign(Paint.Align.CENTER);
        serverTimePaint.setFakeBoldText(true);
        serverTimePaint.setTextSize( dipToPx(getContext(), 15));

    }

    @Override
    protected boolean onDrawSelected(Canvas canvas, Calendar calendar, int x, int y, boolean hasScheme) {
        mSelectedPaint.setStyle(Paint.Style.FILL);
        canvas.drawRect(x + mPadding, y + mPadding, x + mItemWidth - mPadding, y + mItemHeight - mPadding, mSelectedPaint);




        return true;
    }

    /**
     * onDrawSelected
     * @param canvas   canvas
     * @param calendar 日历calendar
     * @param x        日历Card x起点坐标
     * @param y        日历Card y起点坐标
     */
    @SuppressWarnings("IntegerDivisionInFloatingPointContext")
    @Override
    protected void onDrawScheme(Canvas canvas, Calendar calendar, int x, int y) {
        mSchemeBasicPaint.setColor(calendar.getSchemeColor());
        canvas.drawRect(x + mItemWidth / 2 - mW / 2,
                y + mItemHeight - mH * 2 - mPadding,
                x + mItemWidth / 2 + mW / 2,
                y + mItemHeight - mH - mPadding, mSchemeBasicPaint);
    }
    @SuppressWarnings("IntegerDivisionInFloatingPointContext")
    @Override
    protected void onDrawText(Canvas canvas, Calendar calendar, int x, int y, boolean hasScheme, boolean isSelected) {
        int cx = x + mItemWidth / 2;
        int top = y - mItemHeight / 6;
        int piclefttop = x + (mItemWidth / 2)  - (bmpGou.getWidth()/2);
        int picSign  = x + (mItemWidth / 2)  - (bmpSign.getWidth()/2);
        if (hasScheme) {

//            String scheme = bean.getCredit()+"#"+bean.getConfigCredit();
            String[] scheme = calendar.getScheme().split("#");

            if("0".equals(scheme[0])){
                canvas.drawText(String.valueOf(calendar.getDay()), cx, mTextBaseLine + top,
                        calendar.isCurrentDay() ? mCurDayTextPaint :
                                calendar.isCurrentMonth() ? mSchemeTextPaint : mOtherMonthTextPaint);
                //设置 图片 日期底部的 勾
                canvas.drawBitmap(bmpGou, piclefttop,  mTextBaseLine + top + 28 , mPicPaint);



            }else if ("1".equals(scheme[0])){
                //设置签到
                 canvas.drawBitmap(bmpSign,  picSign, y+20 , mPicPaint);
                //设置 积分
                canvas.drawText(scheme[1]+"积分", cx, mTextBaseLine + y + 30, pointPaint);
            }
            //当前日期
            if("3".equals(scheme[0])){
                //当前时间
                String[] da = scheme[1].split("-");
                canvas.drawText(String.valueOf(da[2]), cx, mTextBaseLine + top,
                        serverTimePaint );



                //设置 图片 日期底部的 勾
                canvas.drawBitmap(bmpGou, piclefttop,  mTextBaseLine + top + 28 , mPicPaint);
            }

        } else {

            canvas.drawText(String.valueOf(calendar.getDay()), cx, mTextBaseLine + top,
                    calendar.isCurrentDay() ? mCurDayTextPaint :
                            calendar.isCurrentMonth() ? mCurMonthTextPaint : mOtherMonthTextPaint);
        }
    }

    /**
     * dp转px
     *
     * @param context context
     * @param dpValue dp
     * @return px
     */
    private static int dipToPx(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

}
